  /**************************************************************************/
  /*                                                                        */
  /*  System  Name :  ICIM                                                  */
  /*                                                                        */
  /*  Description  :  RePrint Lot Label Management                          */
  /*                                                                        */
  /*  MODIFICATION HISTORY                                                  */
  /*    Date     Ver     Name          Description                          */
  /* ---------- ----- ----------- ----------------------------------------- */
  /* 2014/06/26 N0.00   meijiao.C      Initial release                      */
  /*                                                                        */
  /**************************************************************************/
  $(document).ready(function() {
	  var label = new Label();
	  var VAL ={
	        NORMAL     : "0000000"  ,
	        EVT_USER   : $("#userId").text(),
	        T_XPINQLOT : "XPINQLOT",
          	T_XPINQWOR : "XPINQWOR"
	  };
	  var controlsQuery = {
		    W                  : $(window)             ,
		    lotidTxt           : $("#lotidTxt"),
			mainGrd:{
				grdId          : $("#shtListGrd"),
				grdPgText      : "#shtListPg"       ,
	            fatherDiv      : $("#shtListDiv")
			},
          print1Select      : $("#print1Select")
		};
	  var btnQuery ={
		  f1    : $("#f1_query_btn"),
		  print : $("#print_btn"),
		  print2 :$("#print_btn2")
	  };
	  var toolFunc = {
		  iniClear:function(){
			controlsQuery.lotidTxt.val("");
			controlsQuery.mainGrd.grdId.jqGrid("clearGridData");
			$("form").submit(function(){
                return false ;
             });
		  }
	  };
	  /**
	     * All button click function
	     * @type {Object}
	     */
	    var btnFunc = {
	        //Query
	        f1_func : function(){
	        	var inObj,outObj;
	        	var lot_id = controlsQuery.lotidTxt.val();
	        	if( !lot_id){
	        		showErrorDialog("","请输入批次号！");
	                return false;
	        	}
	        	var inObj={
	        		trx_id : VAL.T_XPINQWOR,
	        		action_flg : 'C',
                    worder_id : lot_id
	        	}
                var outTrx =  comTrxSubSendPostJson(inObj);
	        	if( outTrx.rtn_code == VAL.NORMAL ){
	        		setGridInfo(outTrx.oary6,"#shtListGrd");

	        	}
	        },
	        print_func:function(){
	        	var rowIds,crGrid,rowData,lot_id; 
	        	crGrid = controlsQuery.mainGrd.grdId;
	        	rowIds = crGrid.jqGrid('getDataIDs');
	        	if(rowIds.length == 0){
	        		showErrorDialog("","请根据批次号查询出产品信息！");
	                return false;
	        	}
                var prdId = [];
                var prdId1 = [];

	        	for(var i = 0; i < rowIds.length; i++){
                    rowData = crGrid.jqGrid('getRowData',rowIds[i]);
                    if(i<=19){
                        prdId.push(rowData.prd_id);
                    }else {
                        prdId1.push(rowData.prd_id);
                    }
				}
                var lotId = rowData.lot_id;
                var woId = rowData.wo_id;
                //根据工单查客户订单 带出厚度
                var so_id = rowData.soId;
                var from_thickness;
                var to_thickness;
                var iary2={
                    wo_id : so_id
                };
                var inObj4 = {
                    trx_id : 'XPAPLYSO',
                    action_flg : 'Q',
                    tbl_cnt : 1,
                    iary : iary2
                };
                var outObj4 = comTrxSubSendPostJson(inObj4);
                if(outObj4.rtn_code == VAL.NORMAL){
                    from_thickness  = outObj4.oary.from_thickness;
                    to_thickness = outObj4.oary.to_thickness;
                }

                var planQty = rowData.plan_qty;
                var mtrlCate = rowData.mtrl_cate;
                var mdl_cate = rowData.mdl_cate;
                var jz ="";
                var cusId = rowData.cus_id;
                var fmMdlIdFk = rowData.fm_mdl_id_fk;
                var thMdlIdFk = rowData.th_mdl_id_fk;
                var prdQty =  rowData.prd_qty;
                var boxId = rowData.box_id;
                var mtrlBoxId = rowData.mtrl_box_id;
                var crtTimestamp = rowData.crt_timestamp;
                var mdlGuige = rowData.mdl_Guige+" "+from_thickness +"->" +to_thickness;
                var layoutIdFk = rowData.layout_id_fk;
                var woCateDsc = rowData.wo_cate_dsc;
                var  mtrl_size=rowData.mtrl_size;
                var oldBox =rowData.oldBox;      // 是否原箱号
                var printObj = {
                    lot_id : lotId, //加工批号
                    woId : woId,    //工单ID
                    planQty : planQty,  //计划数量
                    mtrlCate : mdl_cate,  //属性
                    woCateDsc : woCateDsc,//工单类型
                    jz : mtrl_size,           //机种取消并改为玻璃尺寸
                    cusId : cusId,  //客户代码
                    fmMdlIdFk : fmMdlIdFk,  //未减薄料号
                    thMdlIdFk : thMdlIdFk,  //减薄后料号
                    prdQty : prdQty,   //玻璃数量
                    boxId : boxId,   //包装箱号
                    mtrlBoxId : mtrlBoxId,  //来料箱号
                    crtTimestamp : crtTimestamp,  //投料日期
                    mdlGuige : mdlGuige,   //基板型态
                    layoutIdFk : layoutIdFk,   //排版数
                    oldBox:oldBox,          // 是否原箱
                    prdId : prdId   //玻璃ID
                }

                var printObj1 = {
                    lot_id : lotId+"(2)", //加工批号
                    woId : woId,    //工单ID
                    planQty : planQty,  //计划数量
                    mtrlCate : mdl_cate,  //属性
                    woCateDsc : woCateDsc,//工单类型
                    jz : mtrl_size,           //机种取消并改为玻璃尺寸
                    cusId : cusId,  //客户代码
                    fmMdlIdFk : fmMdlIdFk,  //未减薄料号
                    thMdlIdFk : thMdlIdFk,  //减薄后料号
                    prdQty : prdQty,   //玻璃数量
                    boxId : boxId,   //包装箱号
                    mtrlBoxId : mtrlBoxId,  //来料箱号
                    crtTimestamp : crtTimestamp,  //投料日期
                    mdlGuige : mdlGuige,   //基板型态
                    layoutIdFk : layoutIdFk,   //排版数
                    oldBox:oldBox,          // 是否原箱
                    prdId : prdId1   //玻璃ID
                }

                var printObj2 = {
                    lot_id : lotId, //加工批号
                    woId : woId,     //工单ID
                    planQty : planQty, //计划数量
                    mtrlCate : mdl_cate, //属性
                    jz : jz,    //机种
                    cusId : cusId,   //客户代码
                    fmMdlIdFk : fmMdlIdFk,  //未减薄料号
                    thMdlIdFk : thMdlIdFk, //减薄后料号
                    prdQty : prdQty,   //玻璃数量
                    boxId : boxId,   //包装箱号
                    mtrlBoxId : mtrlBoxId,  //来料箱号
                    crtTimestamp : crtTimestamp,  //投料日期
                    mdlGuige : mdlGuige, //基板型态
                    layoutIdFk : layoutIdFk   //排版数
                }


                try{
                    if(prdQty<=20){
                        label.PrintForYNKHN($("#print1Select").find("option:selected").text(),JSON.stringify(printObj));
                    }else {
                        label.PrintForYNKHN($("#print1Select").find("option:selected").text(),JSON.stringify(printObj));
                        label.PrintForYNKHN($("#print1Select").find("option:selected").text(),JSON.stringify(printObj1));
                    }

                }catch(ex){
                    showMessengerErrorDialog("打印失败!",1);
                    console.error(ex);
                }

                //镀膜才打印
                if(mtrlCate.indexOf("B") != -1){// !=-1含有 ==-1不含有
                    try{
                        label.PrintYNKLCD2($("#print1Select").find("option:selected").text(),JSON.stringify(printObj2));
                    }catch(ex){
                        showMessengerErrorDialog("打印失败!",1);
                        console.error(ex);
                    }
                }
				toolFunc.iniClear();
	  		},

	  		print2_fnc : function (){
                var rowIds,crGrid,rowData,lot_id;
                crGrid = controlsQuery.mainGrd.grdId;
                rowIds = crGrid.jqGrid('getDataIDs');
                if(rowIds.length == 0){
                    showErrorDialog("","请根据批次号查询出产品信息！");
                    return false;
                }
                var prdId = [];
                for(var i = 0; i < rowIds.length; i++){
                    rowData = crGrid.jqGrid('getRowData',rowIds[i]);
                    prdId.push(rowData.prd_id);
                }
                var lot_id = rowData.lot_id;
                var woId = rowData.wo_id;
                var woCateDsc = rowData.wo_cate_dsc;
                var planQty = rowData.plan_qty;
                var mtrlCate = rowData.mdl_cate;
                var jz ="";
                var cusId = rowData.cus_id;
                var fmMdlIdFk = rowData.fm_mdl_id_fk;
                var thMdlIdFk = rowData.th_mdl_id_fk;
                var prdQty =  rowData.prd_qty;
                var boxId = rowData.box_id;
                var mtrlBoxId = rowData.mtrl_box_id;
                var crtTimestamp = rowData.crt_timestamp;
                var mdlGuige = rowData.mdl_Guige;
                var layoutIdFk = rowData.layout_id_fk;

                var printObj = {
                    lot_id : lot_id, //加工批号
                    woId : woId,    //工单ID
                    planQty : planQty,  //计划数量
                    mtrlCate : mtrlCate,  //属性
                    woCateDsc : woCateDsc,//工单类型
                    cusId : cusId,  //客户代码
                    fmMdlIdFk : fmMdlIdFk,  //未减薄料号
                    thMdlIdFk : thMdlIdFk,  //减薄后料号
                    prdQty : prdQty,   //玻璃数量
                    crtTimestamp : crtTimestamp,  //投料日期
                    mdlGuige : mdlGuige,   //基板型态
                    layoutIdFk : layoutIdFk,   //排版数
                    prdId : prdId   //玻璃ID
                }

                try{
                    label.PrintForYNKSC($("#print1Select").find("option:selected").text(),JSON.stringify(printObj));
                }catch(ex){
                    showMessengerErrorDialog("打印失败!",1);
                    console.error(ex);
                }

                toolFunc.iniClear();
            }



	  };
	  var iniGridInfo = function(){
 			var grdInfoCM = [
                {name:"slot_no"     , index:"slot_no"      , label:"子格位" , width :50,sorttype:'integer'},
                {name:"prd_id"     , index:"prd_id"      , label:"玻璃ID" , width :50  },
                {name:"wo_id"     , index:"wo_id"      , label:"工单号" , width :50  },
 				{name:"lot_id"     , index:"lot_id"      , label:"加工批号" , width :50  },
 				{name : "plan_qty"       , index : "plan_qty"        ,label : "工单计划数量",width : 50},
                {name : "wo_cate_dsc"       , index : "wo_cate_dsc"        ,label : "工单类型",width : 50},
	            {name:"mtrl_cate"      , index:"mtrl_cate"       , label:"属性1"   , width :50 ,hidden :true},
                {name:"mtrl_size"      , index:"mtrl_size"       , label:"玻璃尺寸"   , width :50 ,hidden :true},
                {name:"mdl_cate"      , index:"mdl_cate"       , label:"属性"   , width :50 },
	            {name:"cus_id"      , index:"cus_id"       , label:"客户代码"     , width :50 },
				{name:"fm_mdl_id_fk"       , index:"fm_mdl_id_fk"        , label:"未减薄料号"      , width :50 },
				{name:"th_mdl_id_fk"       , index:"th_mdl_id_fk"        , label:"减薄后料号"      , width :50 },
				{name:"prd_qty"       , index:"prd_qty"        , label:"玻璃数量"      , width :50 ,hidden :true},
				{name:"box_id"       , index:"box_id"        , label:"包装箱号"      , width :50 },
                {name:"oldBox"       , index:"oldBox"        , label:"出货方式"      , width :50,hidden :true },
				{name:"mtrl_box_id"       , index:"mtrl_box_id"        , label:"来料箱号"      , width :50 },
				{name:"layout_id_fk"       , index:"layout_id_fk"        , label:"排版数"      , width :50 },
				{name:"mdl_Guige"       , index:"mdl_Guige"        , label:"基板型态"      , width :50 },
				{name:"crt_timestamp"       , index:"crt_timestamp"        , label:"投料日期"      , width :50 },
                {name:"soId"       , index:"soId"        , label:"客户订单"      , width :50,hidden:true }
  		];
	        controlsQuery.mainGrd.grdId.jqGrid({
	              url:"",
			      datatype:"local",
			      mtype:"POST",
			      autowidth:true,//宽度根据父元素自适应
			      height:400,
			      shrinkToFit:true,
			      scroll:false,
			      resizable : true,
			      loadonce:true,
			      fixed: true,
			      jsonReader : {
			            // repeatitems: false
			          },
			      viewrecords : true, //显示总记录数
			      rownumbers  :true ,//显示行号
			      rowNum:50,         //每页多少行，用于分页
			      rownumWidth : 40,  //行号列宽度
			      emptyrecords :true ,
                  sortable:true,
                  sortorder :"asc",
                  sortname:'slot_no',
		          colModel: grdInfoCM
	        });
	  };
	  controlsQuery.lotidTxt.keydown(function(event){
	    	if(event.keyCode == 13){
	    		btnFunc.f1_func();
	    	}
	   });
      function getPrinters(){
          controlsQuery.print1Select.append("<option ></option>");
          if (printers !== undefined) {
              for(var i=0; i<printers.length; i++){
                  controlsQuery.print1Select.append("<option>"+ printers[i] +"</option>");
              }
          }
      }
      getPrinters();

	  /**
	     * Bind button click action
	     */
	    var iniButtonAction = function(){
	        btnQuery.f1.click(function(){
	            btnFunc.f1_func();
	        });
	        btnQuery.print.click(function(){
	            btnFunc.print_func();
	        });
            btnQuery.print2.click(function(){
              btnFunc.print2_fnc();
          });
	    };
	  /**
	     * Ini view and data
	     */
	    var initializationFunc = function(){
	        iniGridInfo();
	        toolFunc.iniClear();
	        iniButtonAction();
	    };
	    initializationFunc();
  });